package com.example.jg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.jg.R;
import com.example.jg.bean.HotBean;

import java.util.List;

public class HotAdatper extends RecyclerView.Adapter<HotAdatper.ViewHolder>{
    private Context context;
    public List<HotBean.RecentBean> data;
    private boolean a;
    private OnContextClickListener onContextClickListener;
    public interface OnContextClickListener{
        void OnContextClickListener(View v,int position);
    }

    public void setOnContextClickListener(OnContextClickListener onContextClickListener) {
        this.onContextClickListener = onContextClickListener;
    }

    public HotAdatper(Context context, List<HotBean.RecentBean> data, boolean a) {
        this.context = context;
        this.data = data;
        this.a = a;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.hotitem, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HotBean.RecentBean recentBean = data.get(position);

        Glide.with(context).load(recentBean.getThumbnail()).into(holder.image);
        holder.title.setText(recentBean.getTitle());
        holder.title1.setText((recentBean.getSec()+0)+"秒");
        if (a==true){
            holder.checkBox.setVisibility(View.INVISIBLE);
        }else if (a==false){
            holder.checkBox.setVisibility(View.VISIBLE);
        }
        holder.checkBox.setChecked(recentBean.isChech());
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b==false){
                    recentBean.setChech(true);
                }else  if (b==true){
                    recentBean.setChech(false);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onContextClickListener.OnContextClickListener(view, position);
            }
        });
    }
    public void sc(){
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isChech()){
                data.remove(i);
                i--;
            }
        }
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView title;
        TextView title1;
        CheckBox checkBox;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image=itemView.findViewById(R.id.iamge);
            title=itemView.findViewById(R.id.title);
            title1=itemView.findViewById(R.id.title1);
            checkBox=itemView.findViewById(R.id.checkBox);
        }
    }
}
